package com.xuyang.other;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.bookmanager.R;

public class LoginDialog extends PopupWindow{
	private View mTxtLogin,mTxtLogout;
	private CheckBox mCheckBox;
	private Dialog mDialog;
	private RequestQueue mQueue;
	private EditText mEditUserName,mEditPassWord;
	private LoginSuccess mLoginSuccess;
	private SharedPreferences mSP;
	public LoginDialog(Context context) {
		View view=LayoutInflater.from(context).inflate(R.layout.layout_login, null);
		init(view);
		setListener();
		
		
	}
	private void setListener() {
		mTxtLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final String userName= mEditUserName.getText().toString();
				final String passWord= mEditPassWord.getText().toString();
				mDialog=new ProgressDialog(getContentView().getContext()).getDialog();
				mDialog.show();
				dismiss();
				String url=Flags.USER_LOGIN.replace("USERNAME",userName)
						.replace("PASSWORD",passWord);
				StringRequest request=new StringRequest(url, new Response.Listener<String>() {
					@Override
					public void onResponse(String str) {
						
						Flags.Level=Integer.parseInt(str);
						mDialog.dismiss();
						if(Flags.Level>=0){
							Toast.makeText(getContentView().getContext(), "登录成功", 0).show();
						}else {
							Toast.makeText(getContentView().getContext(), "登录失败，账号或密码错误", 0).show();
							return ;
						}
						Flags.UserName=userName;
						if (mLoginSuccess!=null) {
							mLoginSuccess.loginSuccess();
						}
						//如果记住密码
						if (mCheckBox.isChecked()) {
							mSP.edit().putString("user_name", userName).commit();
							mSP.edit().putString("password", passWord).commit();
							mSP.edit().putBoolean("checked", true).commit();
						}else {
							mSP.edit().clear().commit();
						}
						
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						mDialog.dismiss();
						Toast.makeText(getContentView().getContext(), "登录失败，请检查网络", 0).show();
					}
				});
				mQueue.add(request);
			}
		});
		mTxtLogout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();	
			}
		});
	}
	private void init(View view) {
		this.setContentView(view);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.MATCH_PARENT);
		this.setFocusable(true);
		this.setBackgroundDrawable(new BitmapDrawable());
		mTxtLogin=view.findViewById(R.id.txt_user_login);
		mTxtLogout=view.findViewById(R.id.txt_user_logout);
		mCheckBox=(CheckBox) view.findViewById(R.id.cb_user_remeber);
		mEditUserName=(EditText) view.findViewById(R.id.edit_user_name);
		mEditPassWord=(EditText) view.findViewById(R.id.edit_password);
		mQueue =Volley.newRequestQueue(getContentView().getContext());
		mSP=getContentView().getContext().getSharedPreferences("user_info", Activity.MODE_PRIVATE);
		String name=mSP.getString("user_name", "");
		String password=mSP.getString("password", "");
		boolean checked=mSP.getBoolean("checked", false);
		mEditUserName.setText(name);
		mEditPassWord.setText(password);
		mCheckBox.setChecked(checked);
	}
	public void show(View parent){
		showAtLocation(parent, Gravity.CENTER, 0, 0);
	}
	public interface LoginSuccess{
		void loginSuccess();
	};
	
	public void setOnLoginSuccess(LoginSuccess loginSuccess){
		mLoginSuccess=loginSuccess;
	}
}
